package com.pocket.insight.base.sql.visitor.oracle;

import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 描述：
 *
 * @author shuize
 * @create 2024/12/14
 */
public interface AddDbPrefixVisitor {
    Logger logger = LoggerFactory.getLogger(AddDbPrefixVisitor.class);

    default boolean visitAndAddDbPrefix(SQLExprTableSource x, String tablePrefix) {
        SQLExpr sqlExpr = x.getExpr();
        if (sqlExpr instanceof SQLIdentifierExpr) {
            SQLIdentifierExpr identifierExpr = (SQLIdentifierExpr)sqlExpr;
            String tableName = identifierExpr.getName();
            String newTableName = tablePrefix + "." + tableName;
            logger.debug("rename {} to {}", tableName, newTableName);
            identifierExpr.setName(newTableName);
        }
        return true;
    }
}
